package com.ykh.mybbsdemo.job;


import com.alibaba.fastjson.JSON;
import com.ykh.mybbsdemo.constant.ArticleConstant;
import com.ykh.mybbsdemo.dao.mapper.ArticleMapper;
import com.ykh.mybbsdemo.dao.pojo.po.Article;
import com.ykh.mybbsdemo.dao.pojo.vo.ArticleScoreVo;
import com.ykh.mybbsdemo.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class TimedCalculation {

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private RedisUtil redisUtil;

    /**
     * 每天凌晨计算前5天的文章热度，并存储到Redis中
     */
//    @Scheduled(cron = "0 * */2 ? * *")
    @Scheduled(cron = "0 0 */2 * * *")
    public void articleCalculation(){
        log.info("定时计算文章热度开始");
        //查询前5天的所有文章
        List<Article> articleList = articleMapper.getxDaysArticle(30);
        //计算文章的阅读量、点赞量、评论量等，按权重计算文章分值
        List<ArticleScoreVo> hotArticleList = computeHotArticle(articleList);
        //排序
        List<ArticleScoreVo> sortedHotArticleList = hotArticleList.stream().sorted(Comparator.comparing(ArticleScoreVo::getScore).reversed()).collect(Collectors.toList());
        if(sortedHotArticleList.size() > 10){
            sortedHotArticleList = sortedHotArticleList.subList(0,10);
        }
        System.out.println(JSON.toJSONString(sortedHotArticleList));
        //分值最高的10条存入Redis
        redisUtil.set(ArticleConstant.HOT_ARTICLE_FIRST_PAGE, JSON.toJSONString(sortedHotArticleList));
        log.info("定时计算文章热度结束");
    }

    private List<ArticleScoreVo> computeHotArticle(List<Article> articleList) {
        List<ArticleScoreVo> hotArticleList = new ArrayList<>();
        if(articleList != null && articleList.size() > 0){
            for(Article article : articleList){
                ArticleScoreVo articleScoreVo = new ArticleScoreVo();
                BeanUtils.copyProperties(article,articleScoreVo);
                //计算文章分值
                Long score = computeArticleScore(articleScoreVo,article);
                articleScoreVo.setScore(score);
                hotArticleList.add(articleScoreVo);
            }
        }
        return hotArticleList;
    }

    private long computeArticleScore(ArticleScoreVo articleScoreVo, Article article) {
        long score = 0;
        if(article.getViewCounts()!=null){
            score += article.getViewCounts();
        }
        if(article.getGoodNum()!=null){
            score += article.getGoodNum() * ArticleConstant.HOT_ARTICLE_LIKE_WEIGHT;
        }
        if(article.getCommentCounts()!=null){
            score += article.getCommentCounts() * ArticleConstant.HOT_ARTICLE_COMMENT_WEIGHT;
        }
        if(article.getCollectNum()!=null){
            score += article.getCollectNum() * ArticleConstant.HOT_ARTICLE_COLLECTION_WEIGHT;
        }
        return score;
    }


    // 视频定时计算
}
